﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_activityManager : BaseListPage
{
  
    protected void Page_Load(object sender, EventArgs e)
    {
        TableName = "Activity"; Pk = "ListId";
       
        if (role.RoleId == 1)
        {
            btnAdd.Visible = false;
            Button1.Visible = false;
        }
        else
        {
            btnAudit.Visible = false;
            loginname.Visible = false;
            textName.Visible = false;
            btnSearch.Visible = false;
            Condition = "";
            Condition = Condition + " and SellerId= " + user.UserId;
        }

        PageSize = 20;
       
        if (!this.IsPostBack)
        {
            PageIndex = 0;
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
        }
    }
    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        PageIndex = Pager.CurrentPageIndex - 1;
        BindData(ref rptList);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Condition = "";
        if (role.RoleId == 1)
        {
            //查询
            string name = textName.Text.Trim();
            try
            {
                if (name != "")
                {
                    Condition = Condition + "and SellerId in (";
                    int id = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == name).UserId;
                    List<Activity> list = ActivityBLL.GetInstance().GetList(r => r.SellerId == id);
                    foreach (var l in list)
                    {
                        Condition = Condition + l.SellerId + " ,";
                    }
                    Condition = Condition.Substring(0, Condition.Length - 1);
                    Condition = Condition + ")";
                }
            }
            catch
            {
                UtilityMessageBox.Show(this, "输入用户名不存在");
            }
        }

        if (role.RoleId == 2) {
            Condition = Condition + " and SellerId = " + user.UserId;
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("activityAdd.aspx");
    }
    protected void btnChangeInfo_Click(object sender, EventArgs e)
    {
        if (SelectId() == null)
        {
            return;
        }
        int listId = Convert.ToInt32(SelectId()[0]);
        Activity a = ActivityBLL.GetInstance().GetEntity(r => r.ListId == listId);
        if (a.Status != 0 && a.Status != 2)
        {
            UtilityMessageBox.Show(this, "活动已经审核过，不能修改");
            return;
        }
        Response.Redirect("activityAlter.aspx?id=" + listId);
    }
    protected void btnAudit_Click(object sender, EventArgs e)
    {
        if (SelectId() == null)
        {
            return;
        }
        if (role.RoleId == 1)
        {
            foreach (string item in SelectId())
            {

                int id = Convert.ToInt32(item);
                Activity a = ActivityBLL.GetInstance().GetEntity(r => r.ListId == id);
                if (a.Status == 0)
                {
                    a.ManagerId = user.UserId;
                    a.Status = 1;
                    bool results = ActivityBLL.GetInstance().Update(a);
                    if (!results)
                    {
                        UtilityMessageBox.Show(this, "数据更新异常");
                    }
                }
            }

            TableName = "Activity"; Pk = "ListId";
            PageIndex = 0;
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
        }
        else
        {
            UtilityMessageBox.Show(this, "没有权限操作");
        }

    }
    private string[] SelectId()
    {
        string[] id = null;
        string ids = "";
        try
        {
            ids = Request["ids"];
            if (ids.Length == 0)
            {
                UtilityMessageBox.Show(this, "未选择");
                return null;
            }
        }
        catch
        {
            UtilityMessageBox.Show(this, "未选择");
            return null;
        }
        id = ids.Split(',');
        return id;
    }
    public string getUserName(object o)
    {
        String result = "异常";
        try
        {
            int id = Convert.ToInt32(o);

            result = SystemUserBLL.GetInstance().GetEntity(r => r.UserId == id).LoginName;
        }
        catch 
        {
            result = "admin";
        }
        return result;
    }

    public string getStatus(object o)
    {
        string result = "异常";
        try
        {
            int id = Convert.ToInt32(o);
            switch (id)
            {
                case 0: result = "待审核"; break;
                case 1: result = "通过审核"; break;
                case 2: result = "审核没通过"; break;
                case 3: result = "活动正在进行"; break;
                case 4: result = "活动结束"; break;
                case 5: result = "活动失败"; break;
                case 6: result = "待发货"; break;
            }
        }
        catch { }
        return result;
    }
    protected void Button22_Click(object sender, EventArgs e)
    {
        Condition = " ";

        if (role.RoleId == 1)
        { 
            Condition = Condition + " and Status = 0";
        }
        else
        {
            Condition = Condition + " and Status = 0 and SellerId = " + user.UserId;
        }    TableName = "Activity"; Pk = "ListId";
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }
    protected void Button11_Click(object sender, EventArgs e)
    {
        
        Condition = " ";
        if (role.RoleId == 1)
        {
            Condition = Condition +  " and Status = 1   ";
        }
        else
        {
            Condition = Condition + " and Status = 1 and SellerId = " + user.UserId;
        }
   
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }

    public int getSuccesssNum()
    {
        int result = 0;
        try
        {
            if(role.RoleId==1)
            {
                result = ActivityBLL.GetInstance().GetList(r => r.Status == 6).Count;
            }
            else if(role.RoleId==2)
            {
                result = ActivityBLL.GetInstance().GetList(r => r.Status == 6&& r.SellerId==user.UserId).Count;
            }
            
        }
        catch(Exception e)
        {
            result = 0;
        }
        return result;
    }

    public int getFailNum()
    {
        int result = 0;
        try
        {
            if (role.RoleId == 1)
            {
                result = ActivityBLL.GetInstance().GetList(r => r.Status == 5).Count;
            }
            else if (role.RoleId == 2)
            {
                result = ActivityBLL.GetInstance().GetList(r => r.Status == 5 && r.SellerId == user.UserId).Count;
            }
        }
        catch (Exception e)
        {
            result = 0;
        }
        return result;
    }

    public int getDoingNum()
    {
        int result = 0;
        try
        {
            if (role.RoleId == 1)
            {
                result = ActivityBLL.GetInstance().GetList(r => r.Status == 3).Count;
            }
            else if (role.RoleId == 2)
            {
                result = ActivityBLL.GetInstance().GetList(r => r.Status == 3 && r.SellerId == user.UserId).Count;
            }
        }
        catch (Exception e)
        {
            result = 0;
        }
        return result;
    }

    public int getNotauditNum()
    {
        int result = 0;
        try
        {
            if (role.RoleId == 1)
            {
                result = ActivityBLL.GetInstance().GetList(r => r.Status == 0).Count;
            }
            else if (role.RoleId == 2)
            {
                result = ActivityBLL.GetInstance().GetList(r => r.Status == 0 && r.SellerId == user.UserId).Count;
            }
        }
        catch (Exception e)
        {
            result = 0;
        }
        return result;
    }
}